#!/bin/bash
set -e
if test -f /etc/redhat-release;then
    if grep -q "^CentOS release 6" /etc/redhat-release;then
        true
    elif grep -q "^CentOS Linux release 7" /etc/redhat-release;then
        true
    elif grep -q "^AlmaLinux release 8" /etc/redhat-release;then
        true
    elif grep -q "^CentOS release 5.11" /etc/redhat-release;then
        true
    else
        echo "Unsupport system."
        exit 1
    fi
else
    echo "Unsupport system."
    exit 1
fi


function restart_rsyncd(){
    pkill rsync
    sleep 3
    rsync --daemon
}

if test -f /etc/rsyncd.conf && grep -q "^log file" /etc/rsyncd.conf;then
    mkdir -p /data/logs/rsyncd
    sed -i 's@^log file.*@log file = /data/logs/rsyncd/rsyncd.log@' /etc/rsyncd.conf
    
    if pgrep rsync > /dev/null;then
        if command -v systemctl > /dev/null;then
            if systemctl is-enabled rsyncd &> /dev/null;then
                systemctl restart rsyncd
            else
                restart_rsyncd
            fi
        else
            restart_rsyncd
        fi
    fi
fi

if test -f /etc/vsftpd/vsftpd.conf && grep -q "^vsftpd_log_file" /etc/vsftpd/vsftpd.conf;then
    mkdir -p /data/logs/vsftpd
    sed -i 's@^vsftpd_log_file.*@vsftpd_log_file=/data/logs/vsftpd/vsftpd.log@' /etc/vsftpd/vsftpd.conf
    sed -i 's@^xferlog_file.*@xferlog_file=/data/logs/vsftpd/xfer.log@'         /etc/vsftpd/vsftpd.conf
fi


mkdir -p /data/ndserver/logrotate
cat > /data/ndserver/logrotate/audit << EOF
/data/logs/rsyncd/rsyncd.log
/data/logs/vsftpd/vsftpd.log
/data/logs/vsftpd/xfer.log
{
    daily
    rotate 90
    missingok
    compress
    create
    copytruncate
    dateext
}
/var/log/secure-*[^z]
{
    daily
    rotate 90
    missingok
    compress
    nocreate
    nocopytruncate
    nodateext
}
EOF



rm -f $0
